<template>
  <div>
    <ul class="tab">
      <li v-for="(item, index) in tab" @click="change(index)" :class="idx == index ? 'on' : ''">{{ item }}</li>
    </ul>
    <div class="app-container">
      <div class="tableWrap">
        <el-table v-loading="loading" :data="reservationList" @selection-change="handleSelectionChange">
          <el-table-column type="index" width="55" align="center" />
          <el-table-column label="会员姓名" align="center" prop="memberName" />
          <el-table-column label="电话号码" align="center" prop="phone" />
          <el-table-column label="充值金额" align="center" prop="rechargeMoney"  />
          <el-table-column label="赠送积分" align="center" prop="integral" />
          <el-table-column label="充值时间" align="center" prop="rechargeTime" />
<!--          <el-table-column label="累计充值" align="center"  >-->
<!--            <template slot-scope="scope">-->
<!--              <span class="money">{{ scope.row.integralJoin}}</span>元-->
<!--            </template>-->
<!--          </el-table-column>-->
          <el-table-column label="累计消费" align="center"  >
            <template slot-scope="scope">
              <span class="money">{{ scope.row.integralJoin}}</span>元
            </template>
          </el-table-column>
          <el-table-column label="消费次数" align="center" prop="integralJoin" />
          <el-table-column label="办卡日期" align="center" prop="reservationTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.reservationTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" >
            <template slot-scope="scope">
              <el-button size="mini" @click="handleUpdate(scope.row)"
                v-hasPermi="['mahjong:reservation:edit']" class="look">查看</el-button>

            </template>
          </el-table-column>
        </el-table>
        <div class="txtcenter">
          <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />
        </div>

      </div>
    </div>



    <!-- 添加或修改会员预约对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="会员姓名">
          <el-select v-model="form.memberId" filterable placeholder="请选择会员" no-match-text="无此人信息,需新建" @change="getname">
            <el-option v-for="dict in alluser" :key="dict.id" :label="dict.name" :value="dict.id"></el-option>
          </el-select>
        </el-form-item>
        <!--<el-form-item label="客人姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入客人姓名" />
        </el-form-item>-->
        <!-- <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>-->
        <el-form-item label="预约麻将类型">
          <el-select v-model="form.type" placeholder="请选择预约麻将类型" @change="loadintegral">
            <el-option v-for="dict in typeOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="积分">
          <el-select v-model="form.integral" multiple @change="integraljoin" placeholder="请选择积分">
            <el-option v-for="dict in integralOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="预约时间" prop="reservationTime">
          <el-date-picker clearable size="small" style="width: 200px" v-model="form.reservationTime" type="datetime"
            placeholder="选择预约时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="phone">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReservation, listalluser, getReservation, delReservation, addReservation, updateReservation, exportReservation } from "@/api/mahjong/reservation";

export default {
  name: "Reservation",
  data() {
    return {
      tab: ['充值排行', '消费排行'],
      idx: 0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 会员预约表格数据
      reservationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 预约麻将类型字典
      typeOptions: [],
      // 积分字典
      integralOptions: [],
      alluser: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        memberId: undefined,
        name: undefined,
        phone: undefined,
        type: undefined,
        reservationTime: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("majong_type").then(response => {
      this.typeOptions = response.data;
    });
    this.getalluser();
  },
  methods: {
    change(index) {

      if (this.idx === index || index === undefined) {
        return false;
      } else {
        this.idx = index;
      }
    },
    // 状态 0正常用户，1非正常用户字典翻译
    likesFormat(row, column) {
      return handlerdata(row.likes, this.likesOptions);
    },
    tabooFormat(row, column) {
      return handlerdata(row.taboo, this.tabooOptions);
    },
    customFormat(row, column) {
      return handlerdata(row.custom, this.customOptions);
    },
    americanMahjongFormat(row, column) {
      return handlerdata(row.americanMahjong, this.americanMahjongOptions);
    },
    ordinaryMahjongFormat(row, column) {
      return handlerdata(row.ordinaryMahjong, this.ordinaryMahjongOptions);
    },
    matchMahjongFormat(row, column) {
      return handlerdata(row.matchMahjong, this.matchMahjongOptions);
    },
    customMahjongFormat(row, column) {
      return handlerdata(row.customMahjong, this.customMahjongOptions);
    },
    playingTimeFormat(row, column) {
      return handlerdata(row.playingTime, this.playingTimeOptions);
    },
    getname(e) {

      for (var i = 0; i < this.alluser.length; i++) {
        var data = this.alluser[i];
        if (data.id == e) {
          this.form.name = data.name;
          return
        }
      }
    },
    getalluser() {
      listalluser().then(response => {
        this.alluser = response.data;

      });
    },
    /** 查询会员预约列表 */
    getList() {
      this.loading = true;
      listReservation(this.queryParams).then(response => {
        this.reservationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 预约麻将类型字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.type);
    },
    // 积分字典翻译
    integralFormat(row, column) {
      return this.selectDictLabel(this.integralOptions, row.integral);
    },
    loadintegral(e) {
      this.getDicts(e).then(response => {
        this.integralOptions = response.data;
      });
    },
    integraljoin(e) {
      this.form.integralJoin = [];
      console.log(e);
      for (var i = 0; i < e.length; i++) {
        for (var i = 0; i < this.integralOptions.length; i++) {
          var data = this.integralOptions[i];
          if (e == data.dictValue) {
            this.form.integralJoin.push(data.dictLabel);
          }
        }
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        memberId: undefined,
        name: undefined,
        phone: undefined,
        type: undefined,
        integral: [],
        remark: undefined,
        reservationTime: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加会员预约";
    },
    coverList(e) {
      if (e) {
        return e.split(",");
      }
      return e
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getReservation(id).then(response => {
        this.form = response.data;
        this.form.integral = this.coverList(this.form.integral);
        this.open = true;
        this.title = "修改会员预约";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      var internal = this.form.integral;
      this.form.integral = this.form.integral.join(",");
      this.form.integralJoin = [];
      for (var i = 0; i < internal.length; i++) {
        for (var j = 0; j < this.integralOptions.length; j++) {
          var data = this.integralOptions[j];
          if (internal[i] == data.dictValue) {
            this.form.integralJoin.push(data.dictLabel);
          }
        }
      }
      this.form.integralJoin = this.form.integralJoin.join(",");
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateReservation(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addReservation(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除会员预约编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delReservation(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(function () { });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有会员预约数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportReservation(queryParams);
      }).then(response => {
        this.download(response.msg);
      }).catch(function () { });
    }
  }
};
</script>
<style>
.pagination-container .el-pagination {
  position: static;
}
</style>
<style scoped rel="stylesheet/scss" lang="scss">
.tableWrap {
  background-color: #fff;
  padding-bottom: 30px;

  .txtcenter {
    text-align: center;
  }
  .money{
    color: #CAAB62;
  }
  .look {
    background-color: #CAAB62;
    color: #fff;
  }

  .delete {
    border-color: #EB2B2B;
    color: #EB2B2B;
  }
}

.tab {
  background-color: #fff;
  padding: 10px 20px;

  li {
    display: inline-block;
    width: 105px;
    height: 40px;
    line-height: 40px;
    text-align: center;
    color: #9C9186;
    font-size: 16px;
    cursor: pointer;

    &.on {
      background-color: #CAAB62;
      color: #fff;
      border-radius: 2px;
    }
  }
}
</style>
